package com.hsnn.medstgmini.pro.project.dao.impl;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.pro.project.dao.BidProjAreaDao;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.AreaUtil;

@Repository
public class BidProjAreaDaoImpl extends BaseHibernateDAO implements BidProjAreaDao {

	@Value("#{projareaProp['projarea.enable']}")
	private Integer projAreaEnable;//配置文件是否启用
	
	@Value("#{projareaProp['projarea.GZJYCG_1']}")
	private String projAreaGZSNCG;//贵州省内
	
	@Value("#{projareaProp['projarea.GZJYCG_2']}")
	private String projAreaGZSWCG;//贵州省外
	
	/**
	 * @category 根据地区保存项目与医疗机构关系
	 * @author 韩守松
	 * @date   2015年12月18日
	 * @param  @param hospInfo
	 */
	@SuppressWarnings("unchecked")
	public void saveProjHospBuArea(HospInfo hospInfo,SysUser user){
		if(projAreaEnable == Status.enabled.getKey()){
			String areaId = hospInfo.getArea().getAreaid();
			areaId = AreaUtil.getAreaSHIID(areaId);
			String[] projAreaArray = projAreaGZSNCG.split(",");
			String projCode = "GZJYCG_2";//省外
			for(int i=0;i<projAreaArray.length;i++){
				if(areaId.equals(projAreaArray[i])){
					projCode = "GZJYCG_1";//省内
				}
			}
			
			StringBuffer projHql = new StringBuffer();
			projHql.append(" select proj_id projId,proj_code projCode,name,starttime,endtime,status projStatus from bid_project where proj_code = :projCode ");
			SQLQuery projQuery = getSession().createSQLQuery(projHql.toString());
			projQuery.setParameter("projCode",projCode);
			projQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			Map<String,Object> map = (Map<String, Object>) projQuery.list().get(0);
			
			StringBuffer hql = new StringBuffer();
		    hql.append("insert into bid_projhospital ");
		    hql.append( " values( ");
		    hql.append(" :proj_hosp_id,:proj_id,:proj_name,:proj_starttime,:proj_endtime,:proj_status,:hosp_code,:status,:upd_datetime,:upd_user ");
		    hql.append("  ) ");
		    SQLQuery query = getSession().createSQLQuery(hql.toString());
		    query.setParameter("proj_hosp_id", UUID.randomUUID().toString());
		    query.setParameter("proj_id", map.get("projId"));
		    query.setParameter("proj_name", map.get("name"));
		    query.setParameter("proj_starttime", map.get("starttime"));
		    query.setParameter("proj_endtime", map.get("endtime"));
		    query.setParameter("proj_status", map.get("projStatus"));
		    query.setParameter("hosp_code", hospInfo.getHospCode());
		    query.setParameter("status", Status.enabled.getKey());
		    query.setParameter("upd_datetime", new Date());
		    query.setParameter("upd_user", user.getId());
		    query.executeUpdate();
		    getSession().flush();
		}
	}
	
}
